package tp.controller.parserXML;

import java.awt.Component;
import java.awt.Menu;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import tp.view.*;
import tp.controller.parserXML.qdxml.DocHandler;
import tp.controller.parserXML.qdxml.QDParser;

/**
 * This class is the most basic possible implementation of the DocHandler class.
 * It simply reports all events to System.out as they occur.
 */
public class LectorDePuntajes implements DocHandler {

	// We only instantiate one copy of the DocHandler
	static LectorDePuntajes reporter = new LectorDePuntajes();

	public ArrayList L;

	// Implementation of DocHandler is below this line
	public void startDocument() {
		//System.out.println("  start document");
	}

	public void endDocument() {
		//System.out.println("  end document");
	}

	public void startElement(String elem, Hashtable h) {
		//System.out.println("    start elem: " + elem);
		String nombre = null;
		String puntos = null;
		Enumeration e = h.keys();
		while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			String val = (String) h.get(key);
			//System.out.println("      " + key + " = " + val);
			if (key.equalsIgnoreCase("Puntaje")) {
				puntos = val;
			}
			if (key.equalsIgnoreCase("Nombre")) {
				nombre = val;
			}
		}
		if ((nombre != null) && (puntos != null)) {
			CopyOfBoardView1.ListaPuntajes.add(new Puntaje(Integer
					.parseInt(puntos), nombre));
		}

	}

	public void endElement(String elem) {
		//System.out.println("    end elem: " + elem);
	}

	public void text(String text) {
		//System.out.println("        text: " + text);
	}

	public static void reportOnFile(String file) throws Exception {
		//System.out.println("===============================");
		//System.out.println("file: " + file);

		// This is all the code we need to parse
		// a document with our DocHandler.
		FileReader fr = new FileReader(file);
		QDParser.parse(reporter, fr);

		fr.close();
	}

	public void leer(String nomArch) throws Exception {
		CopyOfBoardView1.ListaPuntajes
				.removeAll(CopyOfBoardView1.ListaPuntajes);
		reportOnFile(nomArch);
	}
}
